import { getPriceString, getShowString, LineHeight, takePriceList } from "../utils/mirUtil";
import { getArrayItem } from "../utils/utls";
import { PriceData } from "../配置/基础配置";
import { MaxStrengSkillLevel, StrengSkillList, StrengSkillPrice } from "../配置/强化技能";

export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let skillList: Array<any> = StrengSkillList[Player.Job];
    let showList = []
    for (let i = 0; i < skillList.length; i++) {
        let item = skillList[i];
        let lv = 0;
        let skill: TUserMagic = Player.FindSkill(item.skillName);
        if (skill) {
            lv = skill.Strengthen;
        }
        showList.push(
            {
                name: `${item.skillName}(强化${lv}重)`,
                fucName: `强化技能.显示价格(${item.skillName},${item.priceName})`
            }
        )
    }
    let ItemsString = `<{S=这里可以强化技能;C=251;X=0;Y=0;}>`;
    ItemsString += getShowString(showList, 0, 15, 150, LineHeight, 2)
    // if (Player.Job) {
    //     ItemsString += `<{S=转换灵魂火符;C=251;X=0;Y=60;/@强化技能.转换灵魂火符()}>`;
    // }
    Npc.Say(Player, ItemsString)
}

function checkSkill(skillName: string, magic: TUserMagic, play: TPlayObject): boolean {
    if (!magic) {
        play.MessageBox(`请学习${skillName}`)
        return false;
    }
    else {
        let level = magic.Level;
        if (level < 3) {
            play.MessageBox(`请将${skillName}练习到3级`)
            return false;
        }
        else {
            let strengthen = magic.Strengthen;
            if (strengthen >= MaxStrengSkillLevel) {
                play.MessageBox(`${skillName}的强化等级已满,无法强化`)
                return false;
            }
        }
    }
    return true
}

export function 显示价格(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let skillName = Args.Str[0]
    let priceName = Args.Str[1]
    let magic: TUserMagic = Player.FindSkill(skillName)
    let flag = checkSkill(skillName, magic, Player)
    if (flag) {
        let strengthen = magic.Strengthen
        let nextLen = strengthen + 1;
        let price: Array<PriceData> = getArrayItem(StrengSkillPrice[priceName], strengthen) as Array<PriceData>
        let obj = getPriceString(price, 0, 15);
        let ItemsString = `<{S=将${skillName}强化到${nextLen}级需要:;C=251;X=0;Y=0}>`;
        // console.log(obj.str)
        ItemsString += obj.str;
        ItemsString += `<{S=开始强化;C=251;X=0;Y=${obj.y}}/@强化技能.开始强化(${skillName},${priceName})>`
        Npc.Say(Player, ItemsString)
    }

}

export function 开始强化(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let skillName = Args.Str[0]
    let priceName = Args.Str[1]
    let magic: TUserMagic = Player.FindSkill(skillName)
    let flag = checkSkill(skillName, magic, Player)
    if (flag) {
        let strengthen = magic.Strengthen
        let nextLen = strengthen + 1;
        let price: Array<PriceData> = getArrayItem(StrengSkillPrice[priceName], strengthen) as Array<PriceData>
        if (takePriceList(price, Player, "强化技能")) {
            magic.SetStrengthen(nextLen)
            Player.MessageBox(`恭喜你将${skillName}的强化到${nextLen}重`)
            Main(Npc, Player, Args);
        }
    }

}

// export function 转换灵魂火符(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
//     if (Player.Job != 2) {
//         return
//     }
//     let oldSkill: TUserMagic = Player.FindSkill("灵魂火符")
//     if (oldSkill) {
//         let lv = oldSkill.Level;
//         let strlen = oldSkill.Strengthen;
//         Player.DelSkill("灵魂火符")
//         Player.AddSkill("新灵魂火符", lv)
//         let newSkill: TUserMagic = Player.FindSkill("新灵魂火符")
//         if (newSkill) {
//             newSkill.SetStrengthen(strlen)
//         }
//     }
//     else {
//         Player.SendMessage('你没有灵魂火符', 1)
//     }
// }


